import UIKit

class TextTest: UIViewController,UITextFieldDelegate,UITextViewDelegate {
    let textField = UITextField();
    let textView = UITextView();
    let placeholderLabel = UILabel();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground;
        setup();
    }
    private func setup(){
        textField.placeholder = "输入你想要时内容";
        textField.borderStyle = .roundedRect;
        textField.textColor = .darkText;
        textField.font = UIFont.systemFont(ofSize: 12);
        textField.returnKeyType = .done;
        textField.clearButtonMode = .whileEditing;
        textField.delegate = self;
        textField.isSecureTextEntry = true
        
        // 添加图标
        let iconView = UIImageView(image: UIImage(systemName: "person.fill"));
        iconView.tintColor = .gray;
        textField.leftView = iconView;
        textField.leftViewMode = .always;
        //按钮
        let textFieldButton = UIButton(type: .system)
        textFieldButton.tag = 0
        textFieldButton.setImage(UIImage(systemName: "eye.slash"), for: .normal)
        textFieldButton.addTarget(self, action: #selector(textFieldbuttonAction(btn:)), for: .touchUpInside)
        textField.rightView = textFieldButton
        textField.rightViewMode = .always
        
        
        view.addSubview(textField)
        textField.translatesAutoresizingMaskIntoConstraints = false
        
//        NSLayoutConstraint.activate([
//            textField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40),
//            textField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
//            textField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
//            textField.heightAnchor.constraint(equalToConstant: 50)
//        ])
        textField.snp.makeConstraints{ make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalToSuperview().inset(120)
            make.height.equalTo(50)
        }
        //================================================================================================================
        textView.font = UIFont.systemFont(ofSize: 20);
        textView.layer.cornerRadius = 10;
        textView.layer.borderColor = UIColor.black.cgColor;
        textView.layer.borderWidth = 1;
        textView.textContainerInset = UIEdgeInsets(top: 12, left: 8, bottom: 12, right: 8);
        textView.delegate = self;
        // 占位符配置
        placeholderLabel.text = "请输入内容..."
        placeholderLabel.textColor = .lightGray
        placeholderLabel.font = textView.font
        
        textView.addSubview(placeholderLabel);
        view.addSubview(textView);
        placeholderLabel.translatesAutoresizingMaskIntoConstraints = false;
        textView.translatesAutoresizingMaskIntoConstraints = false;
        
//        NSLayoutConstraint.activate([
//            textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 120),
//            textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
//            textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
//            textView.heightAnchor.constraint(equalToConstant: 200),
//            
//            placeholderLabel.topAnchor.constraint(equalTo: textView.topAnchor, constant: 16),
//            placeholderLabel.leadingAnchor.constraint(equalTo: textView.leadingAnchor, constant: 12)
//        ])
        textView.snp.makeConstraints{ make in
            make.top.equalToSuperview().inset(220)
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(200)
        }
        placeholderLabel.snp.makeConstraints{ make in
            make.top.equalToSuperview().inset(12)
            make.left.equalToSuperview().inset(8)
            
        }
        
        let tableBtn = UIButton(frame: CGRect(x: 200, y: 500, width: 150, height: 50))
        tableBtn.backgroundColor = .green
        tableBtn.setTitle("按钮1", for: .normal)
        tableBtn.setTitleColor(.black, for: .normal)
        tableBtn.addTarget(self, action: #selector(clickTableBtn(_:)), for: .touchUpInside)
        self.view.addSubview(tableBtn)
    }
    // MARK: - UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder() // 收起键盘
        print("用户输入: \(textField.text ?? "")")
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // 限制只能输入字母和数字
        let allowedCharacters = CharacterSet.alphanumerics
        let characterSet = CharacterSet(charactersIn: string)
        return allowedCharacters.isSuperset(of: characterSet)
    }
    // MARK: - UITextViewDelegate
    func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !textView.text.isEmpty
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        // 限制最多输入200字符
        let currentText = textView.text ?? ""
        guard let stringRange = Range(range, in: currentText) else { return false }
        let updatedText = currentText.replacingCharacters(in: stringRange, with: text)
        return updatedText.count <= 200
    }
    @objc func clickTableBtn(_ button: UIButton){
        let content = textView.text!
        print(content)
    }
    @objc func textFieldbuttonAction(btn:UIButton){
        if btn.tag == 1{
            btn.setImage(UIImage(systemName: "eye"), for: .normal)
            btn.tag = 0
            textField.isSecureTextEntry = true
        }else{
            btn.setImage(UIImage(systemName: "eye.slash"), for: .normal)
            btn.tag = 1
            textField.isSecureTextEntry = false
        }
    }
}
